/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.functionality;
import java.util.List;

import javax.swing.JComboBox;

import softstarter.gui.SoftStarter;
import softstarter.search.SearchEngine;

/**
 * 
 * @author Arttu Viljakainen
 *
 */
public class ActionsController implements ControllerInterface {

	private static ActionsController SINGLETON = new ActionsController();
	private SoftStarter host; // TODO Make an interface
	private ActionPerformer actionPerformer;
	private SearchEngine searchEngine;
	private FileCentre fileCentre;
	
	private ActionsController() {
		actionPerformer = new ActionPerformer();
		searchEngine = new SearchEngine();
	}
	
	public static ActionsController getInstance(FileCentre fileCentre) { //Bad, very bad.
		SINGLETON.setFileCentre(fileCentre);
		return SINGLETON;
	}
	
	public void setFileCentre(FileCentre fileCentre) {
		this.fileCentre = fileCentre;
	}
	
	@Override
	public void performAction() {
		
		boolean successfullyRanAProgram;
		
		JComboBox commandLine = host.getCommandLine();
		
		System.out.println("Firing action " + commandLine.getSelectedItem().toString() + "!");
		
		String pathToFile = commandLine.getSelectedItem().toString();
		
		successfullyRanAProgram = actionPerformer.performActionFor(pathToFile);
		
		if(!successfullyRanAProgram && commandLine.getItemCount() > 0) {
			pathToFile = ((RunnableItem)commandLine.getItemAt(0)).getFullPath();
			successfullyRanAProgram = actionPerformer.performActionFor(pathToFile);
		}
		
		System.out.println(pathToFile + " executed: " + successfullyRanAProgram);
		if(successfullyRanAProgram) {
			host.setVisible(false);
			host.getCommandLine().setSelectedItem("");
		}
		
	}

	@Override
	public void setHost(SoftStarter host) {
		this.host = host;
	}

	@Override
	public void populateProspectList(String searchword) {
		
		List<RunnableItem> topHits = searchEngine.getTopHits(fileCentre.getFiles(), searchword, 5);
		host.populateCommandLineAndShowPopup(topHits);
	}
	
	@Override
	public SoftStarter getHost() {
		return host;
	}

	@Override
	public FileCentre getFileCentre() {
		return fileCentre;
	}
	
	
	
}
